Besides the common flow of data from Sparkplug edge nodes (and their devices) to the MQTT broker and from there to Sparkplug host applications (consumers), Sparkplug also supports a data flow in the reverse direction: The host applications (consumers) can send data through the MQTT broker to the edge nodes (and their devices). Such Sparkplug commands contain new data for the metrics of the target edge node or device.
Rapid Toolkit for Sparkplug has two groups of methods for publishing Sparkplug commands:
The methods for publishing Sparkplug commands are synchronous, and throw an exception in case of an error. For more information, see Rapid Toolkit for Sparkplug Consumer Error Model.
In order to publish a Sparkplug command containing new data for a single metric, call one of the following methods:
The following example illustrates publishing a single metric to an edge node.
A similar example, but with publishing a value that changes every 2 seconds, is below (this example is useful as a "playground", because the stream of increasing values can be easier observed on the edge node).
Example
The following example illustrates publishing a single metric to a device.
In order to publish a Sparkplug command containing new data for multiple metrics, call one of the following methods:
The following example illustrates publishing multiple metrics to an edge node.
In many cases, Sparkplug commands are send without a timestamp (they contain just the new value of the metric). Missing timestamp is indicated by the default value (DateTime.MinValue) in the Timestamp Property of the SparkplugMetricData object that you pass in the command. If you need to include am explicit timestamp in the Sparkplug command, create the SparkplugMetricData object so that it contains the desired timestamp.
The following example illustrates how a chosen timestamp can be published with the Sparkplug command.
Example
Rapid Toolkit for Sparkplug provides automatic conversion from .NET types to Sparkplug types. The correspondences between the two type systems are described in the article Data Types in Rapid Toolkit for Sparkplug. Therefore, it most cases, publishing the data "just works".
For example, in the following code, Rapid Toolkit for Sparkplug publishes the data with Sparkplug data type Int32Array, because the metric value passed in is a .NET array of Int32 values.
Example
There are cases in which Rapid Toolkit for Sparkplug cannot always determine the desired data type. This is because multiple Sparkplug data types can map to a single .NET type, which you figure out easily by examining the table incldued in the article Data Types in Rapid Toolkit for Sparkplug.
Sparkplug types String and Text are both represented by .NET String type. When Rapid Toolkit for Sparkplug encounters .NET String as a metric value, it chooses Sparkplug String type for it. If your intent was to publish Sparkplug Text type instead, you need to specify the Sparkplug data type explicitly. The following example illustrates how to do it.
Examples
Sparkplug types Bytes and UInt8Array are both represented by .NET array of Byte. When Rapid Toolkit for Sparkplug encounters .NET Byte[] as a metric value, it chooses Sparkplug Bytes type for it, as illustrated in the following example. If you wanted to publish Sparkplug UInt8Array again, you would need to specify the Sparkplug data explicitly, as described above.
Example
Sparkplug is a trademark of Eclipse Foundation, Inc. "MQTT" is a trademark of the OASIS Open standards consortium. Other related terms are trademarks of their respective owners. Any use of these terms on this site is for descriptive purposes only and does not imply any sponsorship, endorsement or affiliation.